package com.kma.summer2012.dao;

import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsRequestsState;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 19:02
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsRequestsStateDAOImpl implements CrumbsRequestsStateDAO {
    @Override
    public void add(CrumbsRequestsState requestsState) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(requestsState);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsRequestsState requestsState) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(requestsState);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsRequestsState getById(int requestsState_id) {
        Session session = null;
        CrumbsRequestsState requestsState = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestsState = (CrumbsRequestsState) session.get(CrumbsRequestsState.class, requestsState_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requestsState;
    }

    @Override
    public Collection<CrumbsRequestsState> getAll() {
        Session session = null;
        List requestsStates = new ArrayList<CrumbsRequestsState>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestsStates = session.createCriteria(CrumbsRequestsState.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requestsStates;
    }

    @Override
    public void delete(CrumbsRequestsState requestsState) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(requestsState);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    public CrumbsRequestsState findByName(String stateName) {
        Session session = null;
        CrumbsRequestsState result = null;
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            result = (CrumbsRequestsState) session.createCriteria(CrumbsRequestsState.class).add(Restrictions.eq("state", stateName)).uniqueResult();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return result;
    }
}
